package it.unibo.mumu.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.util.Log;
import it.unibo.mumu.entity.PlanetId;
import it.unibo.mumu.entity.UniverseSelected;
import it.unibo.mumu.entity.Planet;
import it.unibo.mumu.entity.Image;
import it.unibo.mumu.entity.Audio;
import it.unibo.mumu.entity.Video;
import it.unibo.mumu.util.SingletonSupport;

public class ParseXmlSelectedUniverse {
    
	List<UniverseSelected> universes;
    List<Planet> planets;
    
    private UniverseSelected universe;
    
	public void parse(String filePath) {
    	
    	try{
    		FileInputStream file = new FileInputStream(new File(filePath+"/universes.xml"));
            
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
             
            DocumentBuilder builder =  builderFactory.newDocumentBuilder();
             
            Document xmlDocument = builder.parse(file);

            XPath xPath =  XPathFactory.newInstance().newXPath();
//            String pippo = xmlDocument.getFirstChild().getNodeValue();
//            String expressionTmp = "/universes/universe[@id=19]/description";
            
            
            String expression = "/universes/universe";
            NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
            int IDUniverseRef = -1;
            for (int i = 0; i < nodeList.getLength(); i++) {
            	
            	Node nod = nodeList.item(i);
            	
            	// creo istanza universo
            	universe = new UniverseSelected();
            	
            	// prendo valore dell'ID 
                String id_universe = "@id";
                String idUniverse = xPath.compile(id_universe).evaluate(nod);
                universe.setId(Integer.parseInt(idUniverse));
                IDUniverseRef = Integer.parseInt(idUniverse);
                Log.i("PARS","ID universe = "+idUniverse);
                
                // prendo valore name 
                String name_universe = "@name";
                String nameUniverse = xPath.compile(name_universe).evaluate(nod);
                universe.setName(nameUniverse);
                Log.i("PARS","Name universe = "+nameUniverse);
                
                // prendo valore description 
                String desc_universe = "./description";
                String descUniverse = xPath.compile(desc_universe).evaluate(nod);
                universe.setDescription(descUniverse);
                Log.i("PARS","Descrizione universo = "+descUniverse);
                
                // prendo nodo planet 
                String planetExpr = "./planet";
                NodeList planetList = (NodeList) xPath.compile(planetExpr).evaluate(nod, XPathConstants.NODESET);
                // scorro pianeti
                for (int j = 0; j < planetList.getLength(); j++) {
                	
                	Node planetNode = planetList.item(j);
                	
                	// creo istanza pianeta
                	Planet planet = new Planet();
                	
                	// prendo valore dell'ID pianeta
                    String id_pianeta = "@id";
                    String idPianeta = xPath.compile(id_pianeta).evaluate(planetNode);
                    planet.setIdPlanet(Integer.parseInt(idPianeta));
                    Log.i("PARS","ID pianeta = "+idPianeta);
                    
                    // prendo valore name pianeta
                    String name_pianeta = "@name";
                    String namePianeta = xPath.compile(name_pianeta).evaluate(planetNode);
                    planet.setNamePlanet(namePianeta);
                    Log.i("PARS","Name pianeta = "+namePianeta);
                    
                    // prendo title1 pianeta
                    String title1Expr = "./title1";
                    String title1 = xPath.compile(title1Expr).evaluate(planetNode);
                    planet.setTitle1(title1);
                    Log.i("PARS","Title 1 = "+title1);
                    
                    // prendo title2 pianeta
                    String title2Expr = "./title2";
                    String title2 = xPath.compile(title2Expr).evaluate(planetNode);
                    planet.setTitle2(title2);
                    Log.i("PARS","Title 2 = "+title2);
                    
                    // prendo title3 pianeta
                    String title3Expr = "./title3";
                    String title3 = xPath.compile(title3Expr).evaluate(planetNode);
                    planet.setTitle3(title3);
                    Log.i("PARS","Title 3 = "+title3);
                    
                    // prendo description pianeta
                    String descExpr = "./description";
                    String descPlanet = xPath.compile(descExpr).evaluate(planetNode);
                    planet.setDescription(descPlanet);
                    Log.i("PARS","Desc pianeta = "+descPlanet);
                    
                    // prendo coordX
                    String coordXExpr = "./coordX";
                    String coordX = xPath.compile(coordXExpr).evaluate(planetNode);
                    planet.setCoordX(Float.parseFloat(coordX));
                    Log.i("PARS","X pianeta = "+coordX);
                    
                    // prendo coordY
                    String coordYExpr = "./coordY";
                    String coordY = xPath.compile(coordYExpr).evaluate(planetNode);
                    planet.setCoordY(Float.parseFloat(coordY));
                    Log.i("PARS","Y pianeta = "+coordY);
                    

                   // prendo, se esistono, connessioni a pianeti diversi 
                    String connectionsExpr = "./connections/planetId";
                    NodeList planetIdList = (NodeList) xPath.compile(connectionsExpr).evaluate(planetNode, XPathConstants.NODESET);
                    
                    Log.i("PARS", "Dimensione vett connections: "+planetIdList.getLength());
                    
                    
                    
                    for (int k = 0; k < planetIdList.getLength(); k++) {
					  	Node planetIdNode = planetIdList.item(k);
					  	Log.i("PARS","PlanetID : "+planetIdNode.getFirstChild().getNodeValue());
					  	PlanetId planetId = new PlanetId();
					  	planetId.setPlanetId(Integer.parseInt(planetIdNode.getFirstChild().getNodeValue()));
					  	
					  	String bExpr = "@b";
	                    String b = xPath.compile(bExpr).evaluate(planetIdNode);
	                    planetId.setB(Integer.parseInt(b));
					  	Log.i("PARS", "planetId B = "+b);
					  	
					  	String gExpr = "@g";
	                    String g = xPath.compile(gExpr).evaluate(planetIdNode);
	                    planetId.setG(Integer.parseInt(g));
					  	Log.i("PARS", "planetId G = "+g);
					  	
					  	String rExpr = "@r";
	                    String r = xPath.compile(rExpr).evaluate(planetIdNode);
	                    planetId.setR(Integer.parseInt(r));
					  	Log.i("PARS", "planetId R = "+r);
					  	
					  	String iduniverseExpr = "@iduniverse";
	                    String iduniverse = xPath.compile(iduniverseExpr).evaluate(planetIdNode);
	                    planetId.setIdUniverse(Integer.parseInt(iduniverse));
					  	Log.i("PARS", "planetId iduniverse = "+iduniverse);
					  	
					  	
					  	if(IDUniverseRef == Integer.parseInt(iduniverse)){
					  		// connessione intra-universo
					  		planet.getConnections().add(planetId);
					  	}else{
					  		// connessione extra-universo
					  		planet.getExtraConnections().add(planetId);
					  	}
					  	
					  	
					}
                    
                    
                    
                   // prendo image_set_happy
                  String setHappyExpr = "./image_set_happy/description";
                  String descImgHappy = xPath.compile(setHappyExpr).evaluate(planetNode);
                  planet.getImageSetHappy().setDescription(descImgHappy);
                  Log.i("PARS","Immagini Felici Desc = "+descImgHappy);
                  
                  String imgHappyExpr = "./image_set_happy/image";
                  NodeList imgHappyList = (NodeList) xPath.compile(imgHappyExpr).evaluate(planetNode, XPathConstants.NODESET);
                  ArrayList<Image> setImg = new ArrayList<Image>();
                  for (int l = 0; l < imgHappyList.getLength(); l++) {
                  	Node imgNode = imgHappyList.item(l);
                  	Image img = new Image();
                  	img.setImagePath(imgNode.getFirstChild().getNodeValue());
                  	Log.i("PARS","Image Happy : "+imgNode.getFirstChild().getNodeValue());
                  	setImg.add(img);
                  }
                  planet.getImageSetHappy().setSet(setImg);
                
                
                  // prendo image_set_sad
                  String setSadExpr = "./image_set_sad/description";
                  String descImgSad = xPath.compile(setSadExpr).evaluate(planetNode);
                  planet.getImageSetSad().setDescription(descImgSad);
                  Log.i("PARS","Immagini Tristi Desc = "+descImgSad);
                  
                  String imgSadExpr = "./image_set_sad/image";
                  NodeList imgSadList = (NodeList) xPath.compile(imgSadExpr).evaluate(planetNode, XPathConstants.NODESET);
                  ArrayList<Image> setSadImg = new ArrayList<Image>();
                  for (int l = 0; l < imgSadList.getLength(); l++) {
                  	Node imgNode = imgSadList.item(l);
                  	Image img = new Image();
                  	img.setImagePath(imgNode.getFirstChild().getNodeValue());
                  	Log.i("PARS","Image Sad: "+imgNode.getFirstChild().getNodeValue());
                  	setSadImg.add(img);
                  }
                  planet.getImageSetSad().setSet(setSadImg);
                  
                  
                  // prendo multimedia_happy
                  String multHappyExpr = "./multimedia_happy/description";
                  String descMultHappy = xPath.compile(multHappyExpr).evaluate(planetNode);
                  planet.getMultimediaHappy().setDescription(descMultHappy);
                  Log.i("PARS","Multimedia Happy desc = "+descMultHappy);
                  
                  Audio audioHappy = new Audio();
                  
                  String titleAudioExpr = "./multimedia_happy/audio/@title";
                  String titleAudio = xPath.compile(titleAudioExpr).evaluate(planetNode);
                  audioHappy.setAudioTitle(titleAudio);
                  Log.i("PARS","Multimedia Happy Audio title = "+titleAudio);
                  
                  String pathAudioExpr = "./multimedia_happy/audio";
                  String pathAudio = xPath.compile(pathAudioExpr).evaluate(planetNode);
                  audioHappy.setAudioPath(pathAudio);
                  Log.i("PARS","Multimedia Happy Audio path = "+pathAudio);
                  
                  Video videoHappy = new Video();
                  
                  String titleVideoExpr = "./multimedia_happy/video/@title";
                  String titleVideo = xPath.compile(titleVideoExpr).evaluate(planetNode);
                  videoHappy.setVideoTitle(titleVideo);
                  Log.i("PARS","Multimedia Happy Video title = "+titleVideo);
                  
                  
                  String pathVideoExpr = "./multimedia_happy/video";
                  String pathVideo = xPath.compile(pathVideoExpr).evaluate(planetNode);
                  videoHappy.setVideoPath(pathVideo);
                  Log.i("PARS","Multimedia Happy Video path = "+pathVideo);
                  
                  planet.getMultimediaHappy().setAudio(audioHappy);
                  planet.getMultimediaHappy().setVideo(videoHappy);
                  
                  
               // prendo multimedia_sad
                  String multSadExpr = "./multimedia_sad/description";
                  String descMultSad = xPath.compile(multSadExpr).evaluate(planetNode);
                  planet.getMultimediaSad().setDescription(descMultSad);
                  Log.i("PARS","Multimedia Sad desc = "+descMultSad);
                  
                  Audio audioSad = new Audio();
                  
                  titleAudioExpr = "./multimedia_sad/audio/@title";
                  titleAudio = xPath.compile(titleAudioExpr).evaluate(planetNode);
                  audioSad.setAudioTitle(titleAudio);
                  Log.i("PARS","Multimedia Sad Audio title = "+titleAudio);
                  
                  pathAudioExpr = "./multimedia_sad/audio";
                  pathAudio = xPath.compile(pathAudioExpr).evaluate(planetNode);
                  audioSad.setAudioPath(pathAudio);
                  Log.i("PARS","Multimedia Sad Audio path = "+pathAudio);
                  
                  Video videoSad = new Video();
                  
                  titleVideoExpr = "./multimedia_sad/video/@title";
                  titleVideo = xPath.compile(titleVideoExpr).evaluate(planetNode);
                  videoSad.setVideoTitle(titleVideo);
                  Log.i("PARS","Multimedia Sad Video title = "+titleVideo);
                  
                  pathVideoExpr = "./multimedia_sad/video";
                  pathVideo = xPath.compile(pathVideoExpr).evaluate(planetNode);
                  videoSad.setVideoPath(pathVideo);
                  Log.i("PARS","Multimedia Sad Video path = "+pathVideo);
                  
                  
                  planet.getMultimediaSad().setAudio(audioSad);
                  planet.getMultimediaSad().setVideo(videoSad);
                
                  universe.addPlanet(planet);
                
                }
                
                
                SingletonSupport.getInstance().addUniverse(universe);
                Log.i("DDD", String.valueOf(SingletonSupport.getInstance().getUniverses().size())); 
              
            }
            
            
    	}catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }
 
    
}

